package com.ponhu.ea2.stockcenter.enums;

import org.apache.commons.lang3.ObjectUtils;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 对接OMS,WMS,采购系统事件类型
 */
public enum ExchangeActionTypeEnum {
    /**
     * 1开头为OMS系统使用
     */
    SALES_ORDER(101, "销售下单"),
    CANCEL_SALES_ORDER(102, "取消销售单"),

    /**
     * 2开头为WMS系统使用
     */
    TRANSFER_APPLY(200, "调拨申请", false),
    TRANSFER_OUT(201, "调拨出库", false),
    TRANSFER_IN(202, "调拨入库", true),
    SALES_OUT(203, "销售出库", false),
    SALES_RETURN_IN(204, "销退入库", true),
    PURCHASE_RETURN_OUT(205, "采退出库", false),
    PURCHASE_IN(206, "采购入库", true),
    CHECK_LOSS_OUT(207, "盘亏出库", false),
    CHECK_PROFIT_IN(208, "盘盈入库", true),
    OTHER_IN(209, "其它入库", true),
    OTHER_OUT(210, "其它出库", false),
    TRANSFER_APPLY_CANCEL(211, "取消调拨申请", true),
    PURCHASE_CANCEL_OUT(212, "取消采购入库(出库动作)", false),
    SALES_RETURN_CANCLE_OUT(213, "取消销退入库(出库动作)", false),
    TRANSFER_PLAN_OUT(214, "调拨计划出库", false),
    TRANSFER_PLAN_IN(215, "调拨计划入库", true),

    /**
     * 3开头为采购中心使用
     */
    PURCHASE_RETURN_APPLY(301, "采购退货申请"),
    CANCEL_PURCHASE_RETURN_APPLY(302, "取消采购退货申请");

    private Integer code;

    private String name;

    // 变动类型：true-入库，false-出库
    private Boolean inOut;

    ExchangeActionTypeEnum(Integer code, String name) {
        this.code = code;
        this.name = name;
    }

    ExchangeActionTypeEnum(Integer code, String name, Boolean inOut) {
        this.code = code;
        this.name = name;
        this.inOut = inOut;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Boolean getInOut() {
        return inOut;
    }

    public void setInOut(Boolean inOut) {
        this.inOut = inOut;
    }

    public static ExchangeActionTypeEnum getEnumByCode(Integer code) {
        return Arrays.stream(ExchangeActionTypeEnum.values())
                .filter(em -> em.getCode().equals(code)).findFirst().orElse(null);
    }

    public static List<Integer> getInOutCodes(Boolean inOut) {
        return Arrays.stream(ExchangeActionTypeEnum.values()).filter(em -> ObjectUtils.isNotEmpty(em.getInOut()))
                .filter(em -> !(inOut ^ em.getInOut())).map(ExchangeActionTypeEnum::getCode).collect(Collectors.toList());
    }

}
